<?php

namespace Xrfg\Api\Trading\Account\Resources;

use App\Http\Resources\Resource;
use Illuminate\Support\Facades\Cache;
use Xrfg\Api\Trading\Home\Repositories\CoinPriceRepository;
use Xrfg\Core\Trading\Account\Models\Account;
use Xrfg\Core\Trading\Account\Models\Coin;

class AccountResource extends Resource
{
    /**
     * Transform the resource collection into an array.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function toArray($request)
    {
        // 计算人民币估值
        $number = bcadd($this->available, $this->disabled);
        $coinName = Coin::where('id', $this->coin_id)->first()->name;
        $coin_to_usdt = (new CoinPriceRepository())->coinToUsdt($coinName);
        $number = bcmul($number, $coin_to_usdt);
//        $usdt_to_cny = Cache::get('ticker:okex.com:otc:usdt')['price_cny'];
        $usdt_to_cny = 6.8;
        $cny = bcmul($number, $usdt_to_cny, 2);

        return [
            'id' => $this->id,
            'user_id' => $this->user_id,
            'coin_id' => $this->coin_id,
            'available' => $this->available,
            'disabled' => $this->disabled,
            'cny' => $cny,
            'address' => $this->address,
            'tag' => $this->tag,
            'version' => $this->version,
            'coin' => $this->coin,
        ];
    }
}
